package com.flymobi.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.orm.Persistent;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.Manager;
import com.flymobi.util.ManagerUtil;
import com.flymobi.util.WebContext;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author fly-mobi
 * @created Oct 20, 2012 6:17:40 PM
 * @version $Id: WebAction.java,v 1.1 2010/04/09 03:27:16 airsou Exp $
 */
public abstract class WebAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	protected int page;
	protected int pagePerSize;
	protected int startRow;
	protected String[] selItems;
	protected String key;
	protected String method;
	protected String id;
	protected List<File> upload = new ArrayList<File>();

	protected List<String> uploadContentType = new ArrayList<String>();

	protected List<String> uploadFileName = new ArrayList<String>();

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method
	 *            the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the upload
	 */
	public List<File> getUpload() {
		return upload;
	}

	/**
	 * @param upload
	 *            the upload to set
	 */
	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	/**
	 * @return the uploadContentType
	 */
	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	/**
	 * @param uploadContentType
	 *            the uploadContentType to set
	 */
	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	/**
	 * @return the uploadFileName
	 */
	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	/**
	 * @param uploadFileName
	 *            the uploadFileName to set
	 */
	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @return the selItems
	 */
	public String[] getSelItems() {
		return selItems;
	}

	/**
	 * @param selItems
	 *            the selItems to set
	 */
	public void setSelItems(String[] selItems) {
		this.selItems = selItems;
	}



	@Override
	public String process(WebContext context) {
		if(StringUtil.isEmpty(id)){
			id="-1";
		}
		if (pagePerSize <= 0) {
			pagePerSize = 30;
		}
		if (page <= 0) {
			page = 1;
		}
		startRow = (page - 1) * pagePerSize;
		Manager m = ManagerUtil.getMemberByCookie(context);
		if (m == null) {
			context.put("url", context.getUrl());
			return "login";
		}

		context.put("m", m);
		if (context.getRequest().getParameter("rmsg") != null) {
			context.put("rmsg", context.getRequest().getParameter("rmsg"));
		}
		if (context.getRequest().getParameter("emsg") != null) {
			context.put("emsg", context.getRequest().getParameter("emsg"));
		}
		if (context.getRequest().getParameter("wmsg") != null) {
			context.put("wmsg", context.getRequest().getParameter("wmsg"));
		}
		return webProcess(context, m);
	}

	public abstract String webProcess(WebContext context, Manager m);

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPagePerSize() {
		return pagePerSize;
	}

	public void setPagePerSize(int pagePerSize) {
		this.pagePerSize = pagePerSize;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	protected boolean isEmptyId(Persistent obj) {
		return obj.getId() == null || obj.getId().equals(new Long(0));
	}
}